package com.longdaji.decoration.ui.activitiesOfMine.accountManagement.changePhone;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.DecorationApplication;
import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangePhoneBeforeActivity extends BaseActivity implements ChangePhoneBeforeContract.IView {
    @Inject
    ChangePhoneBeforeContract.IPresenter mPresenter;

    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.et_new_telephone)
    EditText etNewTelephone;
    @BindView(R.id.btn_next_step)
    Button btnNextStep;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone_before);

        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);

        tvTitle.setText("修改手机号");
        initView();

    }

    private void initView() {
        etNewTelephone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() != 0 && !btnNextStep.isEnabled()) {
                    btnNextStep.setEnabled(true);
                } else if (editable.length() == 0) {
                    btnNextStep.setEnabled(false);
                }
            }
        });

    }

    @OnClick({R.id.btn_next_step})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_next_step:
                mPresenter.onNetStepClick(etNewTelephone.getText().toString());
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @Override
    public void showIllegalTelephone() {
        Toast.makeText(this, "不合法的手机号", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showTelephoneVerifyFailed() {
        Toast.makeText(this, "当前号码已被注册", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showNextStep(String newTelephone) {
//        DecorationApplication.addDetroyActivity(this);
        Intent intent = new Intent(this, ChangePhoneActivity.class);
        intent.putExtra("newTelephone", etNewTelephone.getText().toString());
        startActivity(intent);
    }
}
